<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: nengwan_03@163.com
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 用户模型
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-8-17
 +------------------------------------------------------------------------------
 */
namespace Admin\Model;
use Admin\Model\BaseModel;

class CustomerModel extends BaseModel {
    protected $_validate = array(
        array('login_name', 'require', '登录帐号不能为空', self::EXISTS_VALIDATE, 'regex'),
        array('login_name', '', '登录帐号已经存在', self::VALUE_VALIDATE, 'unique'),
    	array('login_name', '1,20', '登录帐号不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('login_pwd', 'require', '登录密码不能为空', self::EXISTS_VALIDATE, 'regex'),
    );

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_BOTH),
    );

    /**
     * 获取用户列表
     * @return array 用户列表数组
     */
    public function lists(){
        
    }

   /**
    * @todo 注册一个用户返回uid
    * @author PHP@妖孽<dino_ma@163.com>
    */
    public function register($login_name=null,$login_pwd=null,$mobile_phone='',$customer_name='',$reg_type=1){
    	$login_len = strlen(trim($login_name));
    	if($login_name =='' || $login_len<1 || $login_len>20){
    		$this->error = '用户名规则不符合';
    		return false;
    	}
    	
    	if(is_null($login_pwd)){
    		$login_pwd = '';//用户默认密码,.先写空后期再考虑
    	}else{
    	    $login_pwd ='';
    		//加密后的密码暂时不考虑先写一个函数准备处理
    	//	$login_pwd = encryptCustomerLoginPwd($login_pwd);//
    	}
    	if(!isset($customer_name) || '' == $customer_name){
    	    $customer_name = '';
    	}
    	//先查用户是否存在.
    	$consumer_info = $this->field('customer_id')->where(array('login_name'=>$login_name))->find();
    	if(!empty($consumer_info)){
    		//存在  返回给报名表uid
    		return $consumer_info['customer_id'];
    	}else{
    		//不存在 写入然后返回给报名表uid
    		$this->startTrans();
    		$customer_id = $this->add(array('login_name'=>$login_name,'mobile_phone'=>$mobile_phone,'reg_type'=>$reg_type,'reg_ip'=>get_client_ip(1)));
    		if(!$customer_id){
    			$this->rollback();
    			$this->error ='新增用户出错~';
    			return false;
    		}
    		//写从表
    		$consumer_detail_data = array('customer_id'=>$customer_id,'customer_name'=>$customer_name);
    		$id = M('CustomerDetail')->add($consumer_detail_data);
    		if(!$id){
    			$this->rollback();
    			$this->error ='用户详细信息写入出错~';
    			return false;
    		}
    		$this->commit();
    		return $customer_id;
    	}
    
    }
    
    /**
     * @todo 后台资源导入--客服系统批量导入一个用户返回
     * @param array() //一个一维数组手机号的集合 需要上一层判断
     * 	<li>$mobile_phone<string11></li>手机号
     *
     * 
     * @param string(1-20) $login_name 登录帐号
     * @param string(1-32) $login_pwd 登录密码(默认传NUll)
     * @param string(11)   $mobile_phone 写入手机号 (这层不做判断,调用这层的地方进行判断)
     * 
     * 
     * @param int 			$reg_type 注册方式这里传2.   (1:后台代报名,2:后台资源导入,3:安卓注册,4:IPhone注册,5:Ipad注册,6:网站注册,7:活动报名,8:收款进入)
     * @return array();
     * <li>status(0,1,2)</li>0失败,1,写入成功,2用户已存在
     * <li>customer_id(int11)</li> 用户id,在status 为1,2的时候会返回
     * <li>info (string)</li>提示信息,在status为0的时候会返回
     * @author  PHP@妖孽<dino_ma@163.com>
     */
    public function importCustomer($mobile_phone_array,$reg_type=2){
    	$num =count($mobile_phone_array);
    	if(!is_array($mobile_phone_array) || $num<=0){
    		$this->error ='数据有误';
    		return false;
    	}
    	//先查一遍数据库,把已有的人Kill掉,之后做写入操作.
    	$old_mobile_phone_array  =$this->field('`mobile_phone`')->where(array('mobile_phone'=>array('in',$mobile_phone_array)))->select();
		if(empty($old_mobile_phone_array)){
			$old_mobile_phone_array = array();
		}
		$old_mobile_phone_array = array_column($old_mobile_phone_array, 'mobile_phone');
    	$data = array();//数据初始化
    	$data = array_diff($mobile_phone_array, $old_mobile_phone_array);
		$data = array_merge($data);
    	$num = count($data);
    	$data_list = array();
    	///--比较之后入库~;
    	if($num == 0){
    		$call_back['status'] = 0;//全部都是重复数据
    		$call_back['data']['num']=0;//
    		return $call_back;
    	}
    	for ($i=0;$i<$num;$i++){
    		$data_list[$i]['login_name']=$data[$i];
    		$data_list[$i]['mobile_phone']=$data[$i];
    		$data_list[$i]['reg_type']=$reg_type;
    		$data_list[$i]['reg_ip']=get_client_ip(1);
    		$data_list[$i]['create_time']=NOW_TIME;
    	}
    	$status = $this->insertIgnoreAddAll($data_list);
    	if(false === $status){
    		$this->error='插入失败';
    		return false;
    	}
    	$call_back = array();
    	if($status !=0){
    		$list = $this->field('`customer_id`,`mobile_phone`')->where(array('mobile_phone'=>array('in',$data)))->select();
    		$call_back['status'] = 1;
    		$call_back['data']['data'] = $list;//返回的数据略多
    		$call_back['data']['num'] = $status;//写入成功了多少条数据
    		
    		$customer_id_list = array_column($list, 'customer_id');
    		M('CustomerDetail')->insertIgnoreAddAll($customer_id_list);
    		return $call_back;
    	}else{
    		$call_back['status'] = 0;//全部都是重复数据
    		$call_back['data']['num']=0;//
    		return $call_back;
    	}
    	
    	
//     	$login_len = strlen(trim($login_name));
//     	if($login_name =='' || $login_len<1 || $login_len>20){
//     		$this->error = '用户名规则不符合';
//     		return false;
//     	}
//     	if(is_null($login_pwd)){
//     		$login_pwd = '';//用户默认密码,.先写空后期再考虑
//     	}else{
//     		//加密后的密码暂时不考虑先写一个函数准备处理
//     		$login_pwd = encryptCustomerLoginPwd($login_pwd);//
//     	}
//     	$call_back = array();
//     	//先查用户是否存在.
//     	$consumer_info = $this->field('customer_id')->where(array('login_name'=>$login_name))->find();
//     	if(!empty($consumer_info)){
//     		//存在  返回给报名表uid
//     		$call_back['status'] = 2;
//     		$call_back['customer_id'] = $consumer_info['customer_id'];
//     		return $call_back;
//     	}else{
//     		//不存在 写入然后返回给报名表uid
//     		$customer_id = $this->add(array('login_name'=>$login_name,'mobile_phone'=>$mobile_phone,'reg_type'=>$reg_type,'reg_ip'=>get_client_ip(1)));
//     		if(!$customer_id){
// //     			$this->rollback();
//     			$call_back['status'] = 0;
//     			$call_back['info'] = '注册失败';
//     			return $call_back;
//     		}
//     		//写从表
// //     		$consumer_detail_data = array('customer_id'=>$customer_id);
// //     		$id = M('CustomerDetail')->add($consumer_detail_data);
// //     		if(!$id){
// //     			$this->error ='用户详细信息写入出错~';
// //     			return false;
// //     		}
// 			//写入成功
//     		$call_back['status'] = 1;
//     		$call_back['customer_id'] = $customer_id;
//     		return $call_back;
//     	}
    }
    
    /**
     * @todo 通过手机号返回用户信息(单条) cache 1小时
     * @param mobile_phone string(11)  手机号
     * @param filed		string 字段
     * return array();
     * @author PHP@妖孽<dino_ma@163.com>
     */
    public function getCustomerByMobilePhone($mobile_phone,$field=''){
		if(!preg_match("/^1[3-8][0-9]{9}$/", $mobile_phone)){
			$this->error = '手机号有误';
			return false;
		}
		if(S('customer_mobile_phone'.$mobile_phone.$field)){
			$info = S('customer_mobile_phone'.$mobile_phone.$field);
		}else{
			$info = $this->where(array('mobile_phone'=>$mobile_phone))->field($field)->find();
			S('customer_mobile_phone'.$mobile_phone.$field,$info,3600);
		}
		return $info;
    }
    
    /**
     * @todo 通过Uid返回用户信息(单条) cache 1小时
     * @param customer_id array  用户Id
     * @param filed		string 字段 
     * return array();
     * @author PHP@妖孽<dino_ma@163.com>
     */
    public function getCustomerByCustomerId($customer_id,$field=''){
//     	$customer_id = (int)$customer_id;
    	if(!is_array($customer_id)){
    		$this->error = '用户id数据有误';
    		return false;
    	}
    	for ($i=0;$i++;$i<count($customer_id)){
    		if((int)$customer_id[$i]==0){
    			unset($customer_id[$i]);
    		}
    	}
    	if(count($customer_id)<=0){
    		$this->error = '请选择用户';
    		return false;
    	}
    	$customer_ids = arr2str($customer_id, ',');
    	if(S('customer_customer_id'.$customer_ids.$field)){
    		$info = S('customer_customer_id'.$customer_ids.$field);
    	}else{
    		$info = $this->where(array('customer_id'=>array('in',$customer_ids)))->field($field)->find();
    		S('customer_customer_id'.$customer_ids.$field,$info,3600);
    	}
    	return $info;
    	
    }
    
    /**
     * @todo 查看用户是否报名
     * @param activity_id int 活动id
     * @param customer  int or string  支持用户id或手机号
     * @param ticket_level int  0:普通票;1:VIP
     */
    public function customerIsJoinActivity($activity_id,$customer,$sign_channel=0,$ticket_level=0){
    	$activity_id = (int)$activity_id;
    	if($activity_id ==0){
    		$this->error ='activity_id参数有误';
    		return false;
    	}
    	if(preg_match("/^1[3-8][0-9]{9}$/", $customer)){
    		//手机号
    		$data['mobile_phone'] = $customer;
    	}else{
    		$customer = (int)$customer;
    		if($customer ==0){
    			$this->error ='customer参数有误';
    			return false;
    		}
    		//---这有问题-----//
    		$customer_info = getCustomerInfo($customer,'mobile_phone');
    		//customer_id
    		$data['mobile_phone'] = $customer_info['mobile_phone'];
    	}
    	$data['customer_name'] ='';
    	$data['activity_id'] =(int)$activity_id;
    	$data['ticket_level']=(int)$ticket_level;
    	$data['sign_channel']=(int)$sign_channel;
    	$obj = D('Admin/Activity','Service');
    	if($customer_id = $obj->joinActivity($data,0)){
    		return $customer_id;
    	}else{
    		$this->error =$obj->getError();
    		return false;
    	}
    }
    
    
    /**
	 * @todo 通过用户手机号获取用户id
	 * @param string(11) mobile_phone 手机号
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
    public function getCustomerIdByMobilePhone($mobile_phone){
    	$call_back = array();
    	if(!preg_match('/^1[3-8][0-9]{9}$/', $mobile_phone) ){
    		$call_back['status'] = 0;
    		$call_back['info'] = '手机号有问题';
    		return $call_back;
    	}
    	$info = $this->field('customer_id')->where(array('mobile_phone'=>$mobile_phone))->find();
    	if(empty($info)){
    		$call_back['status'] = 2;
    		$call_back['info'] = '查无此用户';
    	}else{
    		$call_back['status'] = 1;
    		$call_back['data']['customer_id'] = $info['customer_id'];
    	}
    	return $call_back;
    }
    
    
    /**
     * @todo 验证用户id是否存在
     * @param int(11) customer_id 用户id
     * @author PHP@妖孽 <dino_ma@163.com>
     */
    public function checkCustomerIdIsTrue($customer_id){
    	if((int)$customer_id == 0){
    		$this->error = '用户Id有问题';
    		return false;
    	}
    	$info = $this->field('customer_id')->where(array('customer_id'=>$customer_id))->find();
    	if(empty($info)){
    		$this->error = '暂无此用户';
    		return false;
    	}else{
    		return $info['customer_id'];
    	}
    }

}
